<?php

class ProductCategoryController extends Controller {

    public $layout = 'main';

    /**
     * @var返回过滤数组
     * @author nirsh
     * @version 5.0
     * @demo 
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * @var返回验证条件 如果不需要验证权限 把$isPriv 设置为allow
     * @author nirsh
     * @version 5.0
     * @demo 
     *    switch($action){
     *      case "login":
     *          $isPriv = 'allow'
     *          break;
     *      default:
     *          $isPriv = $this->checkAccess($controller, $action)?'allow':'deny';
     *      }
     */
    public function accessRules() {
        $controller = Yii::app()->getController()->id;
        $action = Yii::app()->getController()->action->id;
        //登录不需要判断权限
        $isPriv = $this->checkAccess($controller, $action);
        return array(
            array($isPriv,
                'actions' => array($action),
                'users' => array('*'),
            )
        );
    }

    /**
     * 访问某条具体的记录.
     * @param $id 某条记录的主键
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    /**
     * 创建动作
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionCreate() {
        $model = new ProductCategory;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['ProductCategory'])) {
            $model->attributes = $_POST['ProductCategory'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    /**
     * 更新数据 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['ProductCategory'])) {
            $model->attributes = $_POST['ProductCategory'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * 删除记录 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionDelete() {
        $sId = $_REQUEST['id'];
        if (is_array($sId)) {
            foreach ($sId as $id) {
                $tModel = $this->loadModel($id);
                $tModel->delete();
                $tModel->deleteAllByAttributes(array('parentid'=>$id));
            }
        } else {
            $tModel = $this->loadModel($sId);
            $tModel->delete();
            $tModel->deleteAllByAttributes(array('parentid'=>$sId));
        }
        if (isset($_REQUEST['ajax'])) {
            echo 1;
        } else {
            $this->redirect($this->rewriteCreateUrl('admin'));
        }
    }

    /**
     * 列表动作 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionIndex() {
        $model = new ProductCategory('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['ProductCategory']))
            $model->attributes = $_REQUEST['ProductCategory'];

        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * 数据管理
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionAdmin() {
        $model = new ProductCategory('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['ProductCategory']))
            $model->attributes = $_REQUEST['ProductCategory'];
        $model->parentid = 0;
        $this->render('admin', array(
            'model' => $model,
        ));
    }
    /**
      +-----------------------------------------------------
     * 说明 查询服务器
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public  function actionGoldPriceAdmin(){
        $model = new ProductCategory('search');
        $model->unsetAttributes();  // clear any default values
        $model->parentid = "-1";
        if (isset($_REQUEST['ProductCategory']))
            $model->attributes = $_REQUEST['ProductCategory'];
        $this->render('admin_price', array(
            'model' => $model,
        ));
    }

    /**
     * 返回数据模型是基于主键所得到的变量 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function loadModel($id) {
        $model = ProductCategory::model()->findByPk((int) $id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * ajax验证
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'poduct-category-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    /**
      +-----------------------------------------------------
     * 说明 获取下级目录
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function actionLowerlevel(){
        $parentid = (int)$_GET['parentid'];
        $model = new ProductCategory;
        $cdb = new CDbCriteria;
        $cdb->order = "title ASC";
        $cdb->compare('parentid', $parentid);
        $data = $model->findAll($cdb);
        $rData = array();
        $jArray = array();
        foreach($data as $sdata){
            $rData['parentid'] = $sdata->parentid;
            $rData['id'] = $sdata->id;
            $rData['title'] = $sdata->title;
            $rData['sort'] = $sdata->sort;
            $rData['price'] =$sdata->price;
            $rData['state'] = $sdata->state;
            $jArray[] = $rData;
        }
        echo CJSON::encode($jArray);
        exit();
    }
     /**
      +-----------------------------------------------------
     * 说明 复制分类 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function actionCopy(){
        $id = $_GET['copyid'];
        $model = new ProductCategory;
        $amodel = $model->findByPk($id);
        $model->attributes = $amodel->attributes;
        $model->isNewRecord = true;
        if($model->save()){     
            echo CJSON::encode($model->attributes);
            exit();
        }
        echo "0";
    }
    /**
      +-----------------------------------------------------
     * 说明添加下级 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function actionAdd(){
        $pid = $_GET['parentid'];
        $model = new ProductCategory;
        $pObj = $model->findByPk($pid);
        $model->parentid = $pid;
        $model->title = "标题";
        $model->sort = 1;
        if($model->save()){ 
            echo CJSON::encode($model->attributes);
        }
    }
    /**
      +-----------------------------------------------------
     * 说明 修改数据
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function actionUpdateChange(){
        $model = $this->loadModel($_GET['id']);
        $fname = $_GET['field'];
        $model->$fname = $_GET['val'];
        if($model->save()){
            echo 1;
            exit();
        }
        echo 0;
    }
    /**
      +-----------------------------------------------------
     * 说明 联动子分类
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function actionGetZone(){
        $parentid = $_REQUEST['gId'];
        $data = ProductCategory::model()->findAll("parentid='".$parentid."'");
        foreach ($data as $value) {
            echo "<option value='".$value->id."'>";
            echo $value->title;
            echo "</option>";
        }
    }
}
